package com.yuanhua.xcxmng.wechat.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yuanhua.xcxmng.wechat.entity.User;
import com.yuanhua.xcxmng.wechat.entity.UserRun;
import com.yuanhua.xcxmng.wechat.entity.UserRunQuery;
import com.yuanhua.xcxmng.wechat.entity.UserTarget;
import com.yuanhua.xcxmng.wechat.mapper.UserMapper;
import com.yuanhua.xcxmng.wechat.mapper.UserRunMapper;
import com.yuanhua.xcxmng.wechat.mapper.UserTargetMapper;
import com.yuanhua.xcxmng.wechat.service.BaseService;
import com.yuanhua.xcxmng.wechat.service.TargetYesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TargetYesServiceImpl extends BaseService implements TargetYesService {

    @Autowired
    private UserMapper       userMapper;
    @Autowired
    private UserRunMapper    userRunMapper;
    @Autowired
    private UserTargetMapper userTargetMapper;

    @Override
    public void targetYes() {
        try {
            List<User> users = userMapper.orderByStep();
            if (CollectionUtil.isNotEmpty(users)) {
                users.stream().forEach(u -> {
                    try {
                        updateTargetYes(u);
                    } catch (Exception e) {
                        logger.error(e,e.getMessage());
                    }
                });
            }
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }

    @Override
    public void targetYes(String openId) {
        try {
            User u = userMapper.findUserByOpenId(openId);
            if (ObjectUtil.isNotNull(u)) {
                updateTargetYes(u);
            }
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }

    private void updateTargetYes(User u) {
        try {
            UserRunQuery param = new UserRunQuery();
            param.setOpenId(u.getOpenId());
            param.setEndDate(DateUtil.endOfDay(new Date()));
            param.setStartDate(DateUtil.beginOfDay(new Date()));
            List<UserRun> userRuns = userRunMapper.selectByOpenIdDateInteval(param);
            UserTarget userTarget = userTargetMapper.selectByOpenId(u.getOpenId());
            if (CollectionUtil.isNotEmpty(userRuns)) {
                UserRun userRun = userRuns.get(0);
                Long step = userRun.getStep() == null ? 0 : userRun.getStep();
                if (userTarget != null) {
                    Long targetStep = userTarget.getTargetStep() != null ? userTarget.getTargetStep() : 0;
                    if (targetStep != 0 && targetStep <= step) {
                        UserRun record = new UserRun();
                        record.setOpenId(u.getOpenId());
                        record.setTargetyes(1L);
                        record.setId(userRun.getId());
                        userRunMapper.updateByPrimaryKeySelective(record);
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e,e.getMessage());
            throw e;
        }
    }
}
